package com.cicoding.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cicoding.bean.File;
import com.cicoding.dao.FileMapper;
import com.cicoding.dto.FileNotice;
import com.cicoding.service.FileService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cicoding.bean.Notice;
import com.cicoding.bean.ShowNotice;
import com.cicoding.constant.state.NoticeType;
import com.cicoding.dao.NoticeMapper;
import com.cicoding.service.NoticeService;
import com.cicoding.utils.BeanUtil;
import com.cicoding.utils.Userinfo;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private FileMapper fileMapper;
    @Autowired
    private BeanUtil beanUtil;

    @Override
    public List<Notice> getAllNotice() {
        return noticeMapper.selectList(null);
    }


    @Override
    public int sendSystemNotice(Notice notice) {

        notice.setCreatetime(new Date());
        notice.setSenduserid(Userinfo.getUser().getId());
        notice.setType(NoticeType.System.getName());
        noticeMapper.add(notice);
        return notice.getId();
    }

    @Override
    public FileNotice getFileNotice(Integer id) {
//        FileNotice fileNotice = new FileNotice();
//        Notice notice = selectById(id);
//        if (notice != null)
//            BeanUtils.copyProperties(notice, fileNotice);
//        else
//            return null;
//        File file = fileMapper.getFileByNoticeId(notice.getId());
//        fileNotice.setFile(file);
        return noticeMapper.getFileNotice(id);
    }

    @Override
    public int sendConmonNotice(String body, String recive) {

        return 0;
    }

    @Override
    public List<ShowNotice> getAllShowNotice() {
        List<Notice> list = this.getIndexNotice();
        List<ShowNotice> showList = new ArrayList<ShowNotice>();
        for (Notice notice : list) {
            showList.add(beanUtil.toShowNotice(notice));
        }
        return showList;
    }


    @Override
    public List<Notice> getIndexNotice() {
        return noticeMapper.selectList(new EntityWrapper<Notice>().orderBy("createtime", false).last("limit 0,15"));
    }


}
